package mop.controller;

import java.util.LinkedList;
import java.util.List;

import mop.exception.IllegalDescriptionException;
import mop.exception.IllegalDuplicationException;
import mop.exception.IllegalIdException;
import mop.model.World;
import mop.model.resource.Resource;
import mop.model.resource.ResourceInterface;
import mop.model.resource.ResourceType;
import mop.model.resource.ResourceTypeInterface;

/**
 * CreateResourceController
 * 
 * Use case controller for the creation of a new resource. 
 * (use case 8)
 * 
 * @author	MOP Team 7
 */
public class CreateResourceController extends UseCaseController
{

	/**
	 * @see UseCaseController#UseCaseController(World)
	 */
	public CreateResourceController(World world)
	{
		super(world);
	}
	
	/**
	 * Get all the possible resource types
	 * 
	 * @return list with interface to all existing resource types
	 */
	public List<ResourceTypeInterface> getResourceTypes()
	{
		List<ResourceTypeInterface> ret = new LinkedList<ResourceTypeInterface>();
		ret.addAll(super.getWorld().getResourceTypeDataBase().fetchAll());
		
		return ret;
	}
	
	
	/**
	 * Use case : Create Resource
	 * 
	 * @param   rti			Resource Type interface 
	 * @param	id			The identifier of the new Resource.
	 * @param	type		A string representing the type of the Resource.
	 * 
	 * @effect	A Resource object is made with the ResourceFactory.
	 * 		| ResourceFactory.createResource(description, type)
	 * @post	The Resource is added to the Resource database.
	 * 		| resourceDatabase.contains(this)
	 * @throws	IllegalArgumentException
	 * 		Thrown when the given resource type is invalid.
	 * @throws  IllegalDescriptionException
	 * 		Thrown when the Resource description is invalid.
	 * @throws 	IllegalDuplicationException 
	 * 		Thrown when a Resource with the same description already exists in the database.
	 */
	public ResourceInterface createResource(ResourceTypeInterface rti,String id) 
		throws IllegalDescriptionException, IllegalDuplicationException, 
		IllegalArgumentException,IllegalIdException
	{
		ResourceType rt = super.getWorld().getResourceTypeDataBase().find(rti.getId());
		Resource newResource = new Resource(rt,id,super.getWorld().getResourceDataBase());
		return newResource;
	}

        /**
         * Returns a list of all resources.
         * 
         * @return  List<ResourceInterface>
         */
        public List<ResourceInterface> getAllResources()
        {
            return super.getAllResources();
        }

	/***************************************************************************
	 * 								Visit
	 ***************************************************************************/
	
	/**
	 * Make a CallBack to the visitor
	 * @param v the visitor
	 */
	public void accept(ControllerVisitor v)
	{
		v.visit(this);
	}
	
	/***************************************************************************
	 * 								Acl
	 ***************************************************************************/
	
	@Override
	public String getAssetId() {
		return "CreateResource";
	}
}